package cn.edu.scau.dormitory_management_system.controller;

import java.net.URL;
import java.net.URLDecoder;
import java.text.Collator;
import java.util.*;

import cn.edu.scau.dormitory_management_system.entity.DormEntity;
import cn.edu.scau.dormitory_management_system.entity.dto.StuDTO;
import cn.edu.scau.dormitory_management_system.service.BedService;
import cn.edu.scau.dormitory_management_system.service.DormService;
import cn.edu.scau.dormitory_management_system.service.DormbuildService;

import cn.edu.scau.dormitory_management_system.service.StudentService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 
 *
 * @author wanglingfu
 * @email 1006340605@qq.com
 * @date 2021-08-02 14:35:10
 */
@RestController
@RequestMapping("/admin/dorm")
public class DormController {
    @Autowired
    DormService dormService;

    @Autowired
    BedService bedService;

    @Autowired
    DormbuildService dormbuildService;

    @Autowired
    StudentService studentService;

    @PostMapping("create")
    public JSONObject create(@RequestBody DormEntity dormEntity){
        boolean flag = dormService.create(dormEntity);
        JSONObject object = new JSONObject();
        if(flag){
            object.put("code", 200);
            object.put("msg", "操作成功");
        }else{
            object.put("code", 0);
            object.put("msg", "操作失败");
        }
        System.out.println(object);
        return object;
    }

    @PostMapping("delete")
    public JSONObject delete(@RequestBody DormEntity dormEntity){
        boolean flag = dormService.delete(dormEntity);
        JSONObject object = new JSONObject();
        if(flag){
            object.put("code", 200);
            object.put("msg", "操作成功");
        }else{
            object.put("code", 0);
            object.put("msg", "操作失败");
        }
        return object;
    }

    @PostMapping("update")
    public JSONObject update(@RequestBody DormEntity dormEntity){
        boolean flag = dormService.update(dormEntity);
        JSONObject object = new JSONObject();
        if(flag){
            object.put("code", 200);
            object.put("msg", "操作成功");
        }else{
            object.put("code", 0);
            object.put("msg", "操作失败");
        }
        return object;
    }

    @PostMapping("findDormByBuild")
    public JSONObject findDormByBuildId(@RequestParam("buildId") int buildId,
                                        @RequestParam(defaultValue = "1") int current,
                                        @RequestParam(defaultValue = "8") int size){
        JSONObject object = new JSONObject();
        IPage<DormEntity> page = new Page<>(current,size);
        QueryWrapper<DormEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("build_id",buildId);
        page = dormService.page(page,wrapper);
        List<DormEntity> dormEntities = dormService.findDormByBuildId(buildId);
        if(page.getRecords().isEmpty()) {
            object.put("code", 0);
            object.put("msg", "无数据");
        }else {
            object.put("code", 200);
            object.put("page", page);
        }
        return object;
    }

    @PostMapping("getAllStudent")
    public JSONObject getAllStudent(@RequestParam("dormId") int dormId,
                                    @RequestParam(defaultValue = "1") int current,
                                    @RequestParam(defaultValue = "8") int size){
        IPage<StuDTO> page = new Page<>(current,size);
        page=dormService.getStuDTO(page,dormId);
        /**
        DormEntity dormEntity = dormService.getById(dormId);
        List<StuDTO> stuDTOS = dormService.getStudentDto(dormEntity);*/
        JSONObject object = new JSONObject();
      //  boolean flag = stuDTOS.isEmpty();
        if(!page.getRecords().isEmpty()){
            object.put("studentDTO", page);
            object.put("msg", "操作成功");
        }else{
            object.put("code", 0);
            object.put("msg", "无数据");
        }
        return object;
    }

    @PostMapping("findDormByDepartment")
    public JSONObject findDormByDepartment(@RequestParam("department") String department1,
                                           @RequestParam(defaultValue = "1") int current,
                                           @RequestParam(defaultValue = "8") int size)throws Exception{
        String department = URLDecoder.decode(department1,"UTF-8").replace("\"","");
        JSONObject object = new JSONObject();
        IPage<DormEntity> page = new Page<>(current,size);
        QueryWrapper<DormEntity> wrapper = new QueryWrapper<>();
        wrapper.like("department",department);
        page = dormService.page(page,wrapper);
        List<DormEntity> dormEntities = dormService.findDormByDepartment(department);
        if(page.getRecords().isEmpty()) {
            object.put("code", 0);
            object.put("msg", "无数据");
        }else {
            object.put("code", 200);
            object.put("page", page);
        }
        return object;
    }

    @PostMapping("findDormByClazz")
    public JSONObject findDormByClazz(@RequestParam("clazz") String clazz1,
                                      @RequestParam(defaultValue = "1") int current,
                                      @RequestParam(defaultValue = "8") int size)throws Exception{
        String clazz = URLDecoder.decode(clazz1,"UTF-8").replace("\"","");
        JSONObject object = new JSONObject();
        IPage<DormEntity> page = new Page<>(current,size);
        QueryWrapper<DormEntity> wrapper = new QueryWrapper<>();
        wrapper.like("clazz",clazz);
        page = dormService.page(page,wrapper);
        List<DormEntity> dormEntities = dormService.findDormByClazz(clazz);
        if(page.getRecords().isEmpty()) {
            object.put("code", 0);
            object.put("msg", "无数据");
        }else {
            object.put("code", 200);
            object.put("page", page);
        }
        return object;
    }

    @PostMapping("findSpareDorm")
    public JSONObject findSpareDorm(@RequestParam("buildId") int buildId,
                                    @RequestParam(defaultValue = "1") int current,
                                    @RequestParam(defaultValue = "8") int size){
        JSONObject object = new JSONObject();
        System.out.println(buildId);
        IPage<DormEntity> page = new Page<>(current,size);
        QueryWrapper<DormEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("status",0);
        wrapper.eq("build_id",buildId);
        page = dormService.page(page,wrapper);
        List<DormEntity> dormEntities = dormService.findSpareDorm(buildId);
        if(page.getRecords().isEmpty()) {
            object.put("code", 0);
            object.put("msg", "无数据");
        }else {
            object.put("code", 200);
            object.put("page", page);
        }
        return object;
    }

    @PostMapping("findDormByName")
    public JSONObject findDormByName(@RequestParam("dormName") String dormName1,
                                     @RequestParam(defaultValue = "1") int current,
                                     @RequestParam(defaultValue = "8") int size) throws Exception{
        String dormName = URLDecoder.decode(dormName1,"UTF-8").replace("\"","");
        JSONObject object = new JSONObject();
        IPage<DormEntity> page = new Page<>(current,size);
        QueryWrapper<DormEntity> wrapper = new QueryWrapper<>();
        wrapper.like("dorm_name",dormName);
        page = dormService.page(page,wrapper);
        DormEntity dormEntity = dormService.findDormByName(dormName);
        if(page.getRecords().isEmpty()) {
            object.put("code", 0);
            object.put("msg", "无数据");
        }else {
            object.put("code", 200);
            object.put("page", page);
        }
        return object;
    }
}
